package com.zryc.shop.sms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zryc.shop.base.Result;
import com.zryc.shop.feign.IFeignProduct;
import com.zryc.shop.pms.entity.Product;
import com.zryc.shop.sms.entity.FlashPromotion;
import com.zryc.shop.sms.entity.FlashPromotionProductRelation;
import com.zryc.shop.sms.mapper.FlashPromotionProductRelationMapper;
import com.zryc.shop.sms.service.IFlashPromotionProductRelationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品限时购与商品关系表 服务实现类
 * </p>
 *
 * @author zhaozhao
 * @since 2024-01-18
 */
@Service
public class FlashPromotionProductRelationServiceImpl extends ServiceImpl<FlashPromotionProductRelationMapper, FlashPromotionProductRelation> implements IFlashPromotionProductRelationService {

    @Autowired
    private IFeignProduct feignProduct;

    /**
     * 分页列表
     *
     * @param pageNum
     * @param pageSize
     * @param flashPromotionId
     * @param flashPromotionSessionId
     * @return
     */
    @Override
    public Page<FlashPromotionProductRelation> list(Integer pageNum, Integer pageSize, Long flashPromotionId, Long flashPromotionSessionId) {
        Page<FlashPromotionProductRelation> page = new Page<>(pageNum, pageSize);
        QueryWrapper<FlashPromotionProductRelation> wrapper = new QueryWrapper<>();
        wrapper.eq("flash_promotion_id", flashPromotionId)
                .eq("flash_promotion_session_id", flashPromotionSessionId);
        page = page(page, wrapper);
        List<FlashPromotionProductRelation> list = page.getRecords();
        int i = 0;
        ArrayList<Long> productIds = new ArrayList<>();
        for (FlashPromotionProductRelation f : list) {
            productIds.add(f.getProductId());
        }
        for (Long pid : productIds) {
            Product product = feignProduct.detail(pid);
            list.get(i).setProduct(product);
            i++;
        }
        page.setRecords(list);
        return page;
    }


    /**
     * 修改秒杀商品信息
     *
     * @param id
     * @param relation
     * @return
     */
    @Override
    public Object updateProduct(Long id, FlashPromotionProductRelation relation) {
        UpdateWrapper<FlashPromotionProductRelation> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        baseMapper.update(relation, updateWrapper);
        return null;
    }
}
